package com.lepu.app.fun.my.bean;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;

@SuppressWarnings("serial")
public class BeanMsgSystem implements Serializable{
	 private static final String TABLE_NAME = "MsgSystem";
	
	//系统消息  、粉丝动态、 公用
	public int id; //数据库主键 
    public String userId;  
    public String messageId ;   
    public String title; 
    public String content; 
    public String time; 
    public String targetType;  //目标类型()
    public String targetId;   //目标id对应type
    public int isRead;  //是否已读
    public String type; //系统or粉丝

    //new add
    public String targetUrl;   //目标url
    
    public static ArrayList<BeanMsgSystem> parseJsonData(String dataString, String type) {
    	ArrayList<BeanMsgSystem> topicList = new ArrayList<BeanMsgSystem>();
        try {
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONArray dataArray = obj1.getJSONArray("ListInfo");

            for(int i=0; i<dataArray.length(); i++){
				JSONObject focusJson = (JSONObject) dataArray.get(i);
				BeanMsgSystem beanMsgSystem = new BeanMsgSystem();
				beanMsgSystem.userId = focusJson.optString("UserID");
				beanMsgSystem.messageId = focusJson.optString("MessageID");
				beanMsgSystem.title = focusJson.optString("MessageTitle");
				beanMsgSystem.content = focusJson.optString("MessageContent");
				beanMsgSystem.time = focusJson.optString("RecordCreateTime"); 
				beanMsgSystem.targetType = focusJson.optString("TargetIDType"); 
				beanMsgSystem.targetId = focusJson.optString("TargetID"); 
				beanMsgSystem.isRead = 0;
				beanMsgSystem.type = type;
				topicList.add(beanMsgSystem);
			}
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return topicList;
    }
    
    //==============================================================
    public static BeanMsgSystem getBeanMsgSystem(BeanMessage beanMessage)
    {
        BeanMsgSystem item = new BeanMsgSystem();

        item.messageId = beanMessage.PSMID;
        item.title = beanMessage.MessageTitle;
        item.content = beanMessage.MessageContent;
        item.time = beanMessage.MessageTime;
        item.targetType = beanMessage.LinkTargetType;
        item.targetId = beanMessage.LinkTargetID;
        item.targetUrl = beanMessage.LinkURL;

        return item;
    }
    //添加
    public static boolean insertMyMessage(BeanMsgSystem item) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("messageId", item.messageId);
        values.put("userId", item.userId);
        values.put("title", item.title);
        values.put("content", item.content);
        values.put("time", item.time);
        values.put("targetType", item.targetType);
        values.put("targetId", item.targetId);
        values.put("isRead", item.isRead);
        values.put("type", item.type);
        values.put("targetUrl", item.targetUrl);

        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }

    //删除某一条记录
    public static boolean deleteItem(String UserID, String msgId) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "userId=? and messageId=?", new String[]{UserID, msgId});

        return flag;
    }

    //删除某一类型所有
    public static boolean deleteAllItem(String UserID, String type) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "userId=? and type=?", new String[]{UserID, type});

        return flag;
    }

    //获取某一类型所有数据
    public static ArrayList<BeanMsgSystem> getMyMessage(String UserID, String type) {
    	ArrayList<BeanMsgSystem> provinces = new ArrayList<BeanMsgSystem>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] { 
					"id","userId", "messageId", "title", "content", "time", "targetType","targetId","isRead","type","targetUrl"},
					"userId=? and type=?",new String[] {UserID, type}, null, null,"time desc");

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }

    /*
     * 当前用户未读数据
     */
    public static int getNewMessageCount(String LoginUserID, int board) {

        int count = 0;
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return count;
        String sql = "select count(*) from MsgSystem where userId=? and type=? and isRead=0";
        String result = dbHelp.executeScalar(sql, new String[]{LoginUserID, String.valueOf(board)});
        count = UtilityBase.parseInt(result);

        return count;
    }

    public static int getNewMessageCount(String LoginUserID) {

        int count = 0;
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return count;
        String sql = "select count(*) from MsgSystem where userId=? and isRead=0";
        String result = dbHelp.executeScalar(sql, new String[]{LoginUserID});
        count = UtilityBase.parseInt(result);

        return count;
    }

    public static boolean updateNewMessageRead(String LoginUserID, int board) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("IsRead", 1);

        flag = dbHelp
                .update(TABLE_NAME, values, "userId=? and type=?", new String[]{
                        LoginUserID, String.valueOf(board)});

        return flag;
    }


    //判断消息是否存在
    public static boolean getMsgIsExist(String LoginUserID, int type, String msgId) {
        boolean isExist = false;
        int count = 0;
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return isExist;

        String sql = "select count(*) from MsgSystem where userId=? and type=? and messageId=?";
        String result = dbHelp.executeScalar(sql, new String[]{LoginUserID, String.valueOf(type), msgId});
        count = UtilityBase.parseInt(result);

        if(count > 0){
            isExist = true;
        }

        return isExist;
    }


	/**
	 * 解析cursor转换为对象
	 */
	private static void cursorParse(ArrayList<BeanMsgSystem> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanMsgSystem beanMsg = new BeanMsgSystem();
			beanMsg.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanMsg.userId = (cursor.getString(cursor.getColumnIndex("userId")));
			beanMsg.messageId = (cursor.getString(cursor.getColumnIndex("messageId")));
			beanMsg.title = (cursor.getString(cursor.getColumnIndex("title")));
			beanMsg.content = (cursor.getString(cursor.getColumnIndex("content")));
			beanMsg.time = (cursor.getString(cursor.getColumnIndex("time")));
			beanMsg.targetType = (cursor.getString(cursor.getColumnIndex("targetType")));
			beanMsg.targetId = (cursor.getString(cursor.getColumnIndex("targetId")));
			beanMsg.isRead = (cursor.getInt(cursor.getColumnIndex("isRead")));
			beanMsg.type = (cursor.getString(cursor.getColumnIndex("type")));
            beanMsg.targetUrl = (cursor.getString(cursor.getColumnIndex("targetUrl")));
			msgArray.add(beanMsg);
		}
	}
	
}
